package com.zjy;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;

/**
 * @author: zjy
 * @createTime: 2021/3/22 16:00
 * @功能：模板生成器
 **/
public class CodeGenerator {

    private static String dataSourceIp = "120.234.23.14";
    private static String dataSourcePort = "13306";
    private static String dataBaseName = "java_code";
    private static String USER_NAME = "root";
    private static String PASSWORD = "Pbids2008.";
    private static String AUTHOR = "zjy";

    private static String basePage = "com.zjy.business";

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void generateCode() {
        String[] modules = scanner("表所属模块（enter键结束输入）").split(",");
        String module = modules[0];
        String entity = basePage + "." + module;
        String[] scanner = scanner("表名（多个,隔开 enter键结束输入）").split(",");
        String url = "jdbc:mysql://" + dataSourceIp + ":" + dataSourcePort +
                "/" + dataBaseName + "?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=Hongkong&nullNamePatternMatchesAll=true&allowPublicKeyRetrieval=true";
        String driverName = "com.mysql.cj.jdbc.Driver";
        AutoGenerator mpg = new AutoGenerator();
        /*配置 GlobalConfig*/
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath + "/business/src/main/java");
        globalConfig.setAuthor(AUTHOR);
        globalConfig.setOpen(false);
        globalConfig.setSwagger2(false);
        globalConfig.setServiceImplName("%sDao");
        mpg.setGlobalConfig(globalConfig);

        /*配置 DataSourceConfig*/
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(url);
        dataSourceConfig.setDriverName(driverName);
        dataSourceConfig.setUsername(USER_NAME);
        dataSourceConfig.setPassword(PASSWORD);
        mpg.setDataSource(dataSourceConfig);

        // 实体类生成配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(entity);
        pc.setEntity("domain.entity");
        pc.setMapper("mapper");
        pc.setServiceImpl("dao");
        mpg.setPackageInfo(pc);

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("templates/entity2.java");
        templateConfig.setMapper("templates/mapper2.java");
        templateConfig.setServiceImpl("templates/dao.java");
        templateConfig.setService(null);
        templateConfig.setController(null);
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude(scanner);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        mpg.execute();
    }

    public static void main(String[] args) {
        generateCode();
    }

}
